package com.jqbd.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jqbd.common.R;
import com.jqbd.dto.ResumeMapData;
import com.jqbd.pojo.Resume;
import com.jqbd.service.ResumeService;
import com.jqbd.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("resume")
public class ResumeController {
    /**
     * 获取前端地图数据
     * @return
     */
    @RequestMapping("getPatientMapData")
    public List<ResumeMapData> getPatientMapData(){
        return resumeService.getResumeMapData();
    }

    @Autowired
    private ResumeService resumeService;

//    @RequestMapping("findAll")
//    public List<Resume> findAll() {
//        return resumeService.findAll();
//    }

    @RequestMapping("findById")
    public String findById(String id) {
        resumeService.findById(id);
        return "success";
    }

    @RequestMapping("add")
    public void add(Resume resume) {
        System.out.println(resume);
        resume.setCreateTime(new Date());
        resume.setUpdateTime(new Date());
        resumeService.add(resume);
    }
    @RequestMapping("deleteById")
    public R deleteById(int id) {
        int i = resumeService.deleteById(id);

        if (i > 0){
            return R.ok();
        } else {
            return R.error();
        }
    }
    @RequestMapping("update")
    public String update(Resume resume) {
       resumeService.update(resume);
         return"success";

    }
    @RequestMapping("findByPage")
    public JsonResult<Resume> findByPage(
            @RequestParam Map<String, Object> map
    ){

        int pageNum = Integer.parseInt(map.get("page") + "");

        int pageSize = Integer.parseInt(map.get("limit") + "");

        PageHelper.startPage(pageNum,pageSize);

        List<Resume> resumesList= resumeService.findAll(map);

        PageInfo<Resume> resumePageInfo = new PageInfo<>(resumesList);

        JsonResult jsonResult = JsonResult.ok();

        jsonResult.setCount(resumePageInfo.getTotal());

        jsonResult.setData(resumePageInfo.getList());
        System.out.println(resumeService.findAll(map));
        System.out.println(jsonResult);
        return jsonResult;
    }

    @RequestMapping("deleteByIds")
    public R deleteByIds(List<String> ids) {
         int i = resumeService.deleteByIds(ids);
        return i > 0? R.ok(): R.error();
    }
    /**
     * 导出Excel
     * @param ids
     * @param response
     */
    @RequestMapping("writeExcel")
    public R writeExcel(Integer [] ids, HttpServletResponse response){
        //System.out.println(Arrays.toString(ids));
        resumeService.writeExcel(ids,response);
        return R.ok();
    }

    /**
     * 导入Excel
     * @param file
     * @return
     *
     * LayUI中默认上传文件名为：file
     */
    @RequestMapping("upload")
    public JsonResult upload(MultipartFile file){
        //判断
        if(file != null){
            System.out.println("上传的文件名：" + file.getOriginalFilename());

            resumeService.save(file);

            JsonResult jsonResult = JsonResult.ok();
            return jsonResult;
        }

        throw new RuntimeException("上传文件不能为空！");
    }
    @RequestMapping("/createdate")
    public R createdate()  {
        Map<Object,Object> map =resumeService.stat();
        return R.ok().data(map);
    }

}